﻿using System;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace PreContractWeb
{
    public class BaseFunction
    {
        public const int CONST_Million = 1170000;

        public const int CONST_PROBLEMLV2_DICTYPEID = 11;

        public const int CONST_PRECONTRACT_COLUMNS_DICTYPE = 13;

        public const string CONST_GRIDSIZE_JSSCRIPT = "<script>ResizeDivForScroll('divGrid');ResizeDivForScroll('divGridTwo');</script>";

        public static void BindContractStatusDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IDicItem itemservice = (BEGDC.PreContract.BLL.IDicItem)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicItem_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.DicItem.CONST_DicItemID;

            droplist.DataTextField = BEGDC.PreContract.DAL.DicItem.CONST_DicItemDesc;

            droplist.DataSource = itemservice.GetDicItemByDicType(BEGDC.PreContract.BLL.DicType.DictionaryType.ContractStatus);

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        public static void BindContractTypeDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IDicItem itemservice = (BEGDC.PreContract.BLL.IDicItem)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicItem_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.DicItem.CONST_DicItemID;

            droplist.DataTextField = BEGDC.PreContract.DAL.DicItem.CONST_DicItemDesc;

            droplist.DataSource = itemservice.GetDicItemByDicType(BEGDC.PreContract.BLL.DicType.DictionaryType.ContractType);

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        public static void BindContractAdjustDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IDicItem itemservice = (BEGDC.PreContract.BLL.IDicItem)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicItem_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.DicItem.CONST_DicItemID;

            droplist.DataTextField = BEGDC.PreContract.DAL.DicItem.CONST_DicItemDesc;

            droplist.DataSource = itemservice.GetDicItemByDicType(BEGDC.PreContract.BLL.DicType.DictionaryType.ContractAdjust);

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Contract Adjust";

                droplist.Items.Insert(0, new ListItem(firstitemText, "-1"));
            }
        }

        public static void BindProvinceDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText, string employeeId)
        {
            BEGDC.PreContract.BLL.IProvince service = (BEGDC.PreContract.BLL.IProvince)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Province_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.Province.CONST_ProvinceID;

            droplist.DataTextField = BEGDC.PreContract.DAL.Province.CONST_ProvinceCName;

            droplist.DataSource = service.GetProvince_DataTable(string.Format("  (ProvinceID IN (SELECT     ProvinceID  FROM          ProvinceAssignment  WHERE      (EmployeeID = {0}))) ", employeeId));

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        public static void BindProvinceDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IProvince service = (BEGDC.PreContract.BLL.IProvince)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_Province_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.Province.CONST_ProvinceID;

            droplist.DataTextField = BEGDC.PreContract.DAL.Province.CONST_ProvinceCName;

            droplist.DataSource = service.GetProvince_DataTable("  ");

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        public static void BindClusterDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IProvinceCluster service = (BEGDC.PreContract.BLL.IProvinceCluster)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ProvinceCluster_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.ProvinceCluster.CONST_ProvinceClusterID;

            droplist.DataTextField = BEGDC.PreContract.DAL.ProvinceCluster.CONST_ClusterCName;

            droplist.DataSource = service.GetProvinceCluster_DataTable("  ");

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, "-1"));
            }
        }

        public static void BindCATDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText, string employeeId)
        {
            BEGDC.PreContract.BLL.ICAT service = (BEGDC.PreContract.BLL.ICAT)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_CAT_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.CAT.CONST_CATID;

            droplist.DataTextField = BEGDC.PreContract.DAL.CAT.CONST_CATCode;

            droplist.DataSource = service.GetCAT_DataTable(string.Format("  (CATID IN (SELECT     CATID  FROM          CATAssignment  WHERE      (EmployeeID = {0}))) ", employeeId));

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        public static void BindCATDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.ICAT service = (BEGDC.PreContract.BLL.ICAT)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_CAT_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.CAT.CONST_CATID;

            droplist.DataTextField = BEGDC.PreContract.DAL.CAT.CONST_CATCode;

            droplist.DataSource = service.GetCAT_DataTable(" ");

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }


        public static void BindBDDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText, string employeeId)
        {
            BEGDC.PreContract.BLL.IBD service = (BEGDC.PreContract.BLL.IBD)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BD_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.BD.CONST_BDID;

            droplist.DataTextField = BEGDC.PreContract.DAL.BD.CONST_BDCode;

            droplist.DataSource = service.GetBD_DataTable(string.Format("  (BDID IN (SELECT     BDID  FROM          BDAssignment  WHERE      (EmployeeID = {0}))) ", employeeId));

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        public static void BindBDDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IBD service = (BEGDC.PreContract.BLL.IBD)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BD_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.BD.CONST_BDID;

            droplist.DataTextField = BEGDC.PreContract.DAL.BD.CONST_BDCode;

            droplist.DataSource = service.GetBD_DataTable(" ");

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        public static void BindBGDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IBG service = (BEGDC.PreContract.BLL.IBG)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BG_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.BG.CONST_BGID;

            droplist.DataTextField = BEGDC.PreContract.DAL.BG.CONST_BGCode;

            droplist.DataSource = service.GetBG_DataTable("  ");

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        public static void BindTradeMonthDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.ICloseDate service = (BEGDC.PreContract.BLL.ICloseDate)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_CloseDate_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.CloseDate.CONST_Month;

            droplist.DataTextField = BEGDC.PreContract.DAL.CloseDate.CONST_Month;

            droplist.DataSource = service.GetCloseDateList();

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }


        public static string GetAlertByReturnValue(bool isCloseWindow)
        {

            return GetAlertByReturnValue(isCloseWindow, string.Empty, BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess));
        }

        public static string GetAlertByReturnValue(bool isCloseWindow, string script)
        {
            return GetAlertByReturnValue(isCloseWindow, script, BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess));
        }

        public static string GetAlertByReturnValue(bool isCloseWindow, string script, string successmessage)
        {

            string strRet = "<script defer>alert('{0}');</script>";

            string strRetCloseWin = "<script defer>alert('{0}');" + script + "window.close();</script>";

            if (!isCloseWindow)
                strRet = string.Format(strRet, successmessage);
            else
                strRet = string.Format(strRetCloseWin, successmessage);

            return strRet;

        }

        public static string ChangeAlertMessage(string alertMessage)
        {
            return alertMessage.Replace("'", "\\'").Replace("\n", "\\n");
        }

        public static void CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType operationtype, string logcontent)
        {
            CreateLog(operationtype, logcontent, System.Web.HttpContext.Current.User.Identity.Name);

        }

        public static void CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType operationtype, string logcontent, string employeename)
        {
            BEGDC.PreContract.DAL.OperationLog log = new BEGDC.PreContract.DAL.OperationLog();

            log.OperationType = operationtype.ToString();

            log.LogContent = logcontent;

            log.OperationDate = System.DateTime.Now;

            log.EmployeeName = employeename;

            if (System.Web.HttpContext.Current == null)
            {
                log.HostName = string.Empty;
                log.HostIP = string.Empty;
            }
            else
            {
                log.HostName = System.Web.HttpContext.Current.Request.UserHostName;
                log.HostIP = System.Web.HttpContext.Current.Request.UserHostAddress;
            }


            log.Create();

        }

        //Added by Edwin.fan 2008-9-28
        //Get all CAT for changing CAT assignment
        public static void BindActiveCATDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.ICAT service = (BEGDC.PreContract.BLL.ICAT)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_CAT_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.CAT.CONST_CATID;

            droplist.DataTextField = BEGDC.PreContract.DAL.CAT.CONST_CATCode;

            droplist.DataSource = service.GetCAT_DataTable("Active = 1");

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        //Added by Edwin.fan 2008-9-28
        //Get all BD for changing BD assignment
        public static void BindActiveBDDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IBD service = (BEGDC.PreContract.BLL.IBD)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BD_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.BD.CONST_BDID;

            droplist.DataTextField = BEGDC.PreContract.DAL.BD.CONST_BDCode;

            droplist.DataSource = service.GetBD_DataTable("Active = 1");

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        //Added by Edwin.fan 2008-9-29
        //Get all report type
        public static void BindReportTypeDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IReportHistory service = (BEGDC.PreContract.BLL.IReportHistory)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ReportHistory_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.ReportHistory.CONST_ReportType;

            droplist.DataTextField = BEGDC.PreContract.DAL.ReportHistory.CONST_ReportType;

            droplist.DataSource = service.GetReportType();

            droplist.DataBind();
            if (hasFirstText)
            {
                if (firstitemText == string.Empty)
                    firstitemText = "Please select";

                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        /// <summary>
        /// Export datagrid to excel
        /// </summary>
        /// <param name="page">source web page</param>
        /// <param name="datagrid">source datagrid</param>
        /// <param name="filename">export file name</param>
        public static void ExportToExcel(System.Web.UI.Page page, GridView gv, string filename)
        {
            //foreach (System.Web.UI.WebControls.DataControlField col in gv.Columns)
            //{
            //    if (col.HeaderText == "Action" || col.HeaderText == "Active" 
            //        || col.HeaderText == "PreContractID"
            //        || col.HeaderText == "PreContractNo"
            //        || col.HeaderText == "")
            //        col.Visible = false;

            //}


            page.Response.Clear();
            page.Response.Buffer = true;
            // Set the content type to Excel.
            page.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
            // Remove the charset from the Content-Type header.
            page.Response.ContentEncoding = System.Text.Encoding.UTF8;
            //page.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>"); 

            page.Response.ContentType = "application/vnd.ms-excel";


            // Turn off the view state.
            page.EnableViewState = false;

            //System.IO.StringWriter tw = new System.IO.StringWriter();
            //System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            StringBuilder sb = new StringBuilder();

            sb.Append("<table  border='1'>");
            sb.Append("<tr>");
            for (int i = 0; i < gv.Columns.Count; i++)
            {
                if (i > 2)
                {
                    //if (i == 20) //15 -> 21
                    //    sb.Append("<td width='400' align='center'><b>" + gv.HeaderRow.Cells[i].Text.ToString() + "</b></td>");
                    //else if (i == 19)
                    //    sb.Append("<td width='400' align='center'><b>" + gv.HeaderRow.Cells[i].Text.ToString() + "</b></td>");
                    //else
                    sb.Append("<td align='center'><b>" + gv.HeaderRow.Cells[i].Text.ToString() + "</b></td>");
                }
            }
            sb.Append("</tr>");

            foreach (GridViewRow gr in gv.Rows)
            {
                sb.Append("<tr>");
                for (int i = 0; i < gr.Cells.Count; i++)
                {
                    if (i > 2)
                    {
                        //yuliang
                        //if (i == 5)
                        //    sb.Append("<td>" + gr.Cells[1].Text.ToString() + "</td>");
                        //else
                        sb.Append("<td>" + gr.Cells[i].Text.ToString() + "</td>");
                    }
                }
                sb.Append("</tr>");
            }
            sb.Append("</table>");

            //gv.RenderControl(hw);
            // Write the HTML back to the browser.

            //page.Response.Write(tw.ToString());
            page.Response.Write(sb.ToString());

            // End the response.
            page.Response.End();

        }

        public static void SendMail(string to, string cc, string from, string subject, string body)
        {

            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(System.Configuration.ConfigurationManager.AppSettings["SmtpServer"], 25);
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.Body = body;
            if (to == string.Empty || to == null)
                return;
            msg.To.Add(to);
            if (cc != string.Empty)
            {
                //cc = "joan.lu@bearingpoint.com";
                //msg.CC.Add(cc);
                //cc = "edwin.fan@bearingpoint.com";
                //msg.CC.Add(cc);
                //cc = "Jun.f.Wu@alcatel-sbell.com.cn";
                //msg.CC.Add(cc);
                //cc = "Yiyan.Huang@alcatel-sbell.com.cn";
                //msg.CC.Add(cc);
            }
            msg.From = new System.Net.Mail.MailAddress(from);
            msg.Subject = subject;
            msg.IsBodyHtml = true;
            client.Send(msg);

            //System.IO.StreamWriter writer = new System.IO.StreamWriter(@"D:\a.htm");
            //writer.Write(body);
            //writer.Close();
        }

        public static string CreateEmail(string ids, string name, string content)
        {
            string emailPath = HttpRuntime.AppDomainAppPath + @"\EmailTemplate\Mail.htm";
            System.IO.StreamReader reader = new System.IO.StreamReader(emailPath, System.Text.Encoding.UTF8);
            string emailHtm = (String)reader.ReadToEnd();

            reader.Close();

            emailHtm = emailHtm.Replace("${Name}", name);
            emailHtm = emailHtm.Replace("${Content}", content);

            int trStart = emailHtm.IndexOf("${TR_START}");
            int trEnd = emailHtm.IndexOf("${TR_END}");

            string emailList = String.Empty;
            string emails = emailHtm.Substring(trStart, (trEnd - trStart + 9));

            string[] id = ids.Split(',');

            for (int i = 0; i < id.Length; i++)
            {
                if (id[i] == String.Empty || id[i] == null)
                    break;

                BEGDC.PreContract.BLL.IPreContractInfo contractservice = (BEGDC.PreContract.BLL.IPreContractInfo)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfo_Service];
                System.Data.DataTable dt = contractservice.GetPreContractInfo(String.Format("PreContractID = '{0}'", id[i]));

                string email = emailHtm.Substring(trStart, (trEnd - trStart));

                //email = email.Replace("${PreContractType}", dt.Rows[0]["PreContractType"].ToString());
                //email = email.Replace("${PreContractStatus}", dt.Rows[0]["PreContractStatus"].ToString());
                email = email.Replace("${PreContractNo}", dt.Rows[0]["PreContractNo"].ToString());
                email = email.Replace("${BDCode}", dt.Rows[0]["BDCode"].ToString());
                email = email.Replace("${ContractName}", dt.Rows[0]["ContractName"].ToString());
                email = email.Replace("${ProductName}", dt.Rows[0]["ProductName"].ToString());
                email = email.Replace("${ProvinceCName}", dt.Rows[0]["ProvinceCName"].ToString());
                email = email.Replace("${CATCode}", dt.Rows[0]["CATCode"].ToString());

                if (dt.Rows[0]["OrionME"] == null || dt.Rows[0]["OrionME"].ToString() == String.Empty)
                    email = email.Replace("${OrionME}", String.Empty);
                else
                    email = email.Replace("${OrionME}", Convert.ToDouble(dt.Rows[0]["OrionME"].ToString()).ToString("N2"));

                //email = email.Replace("${TradeMonth}", dt.Rows[0]["TradeMonth"].ToString());

                //if (dt.Rows[0]["CurrentSituation"] == null)
                //    email = email.Replace("${CurrentSituation}", String.Empty);
                //else
                //    email = email.Replace("${CurrentSituation}", dt.Rows[0]["CurrentSituation"].ToString());

                //if (dt.Rows[0]["BlackDate"] == null || dt.Rows[0]["BlackDate"].ToString() == String.Empty)
                //    email = email.Replace("${BlackDate}", String.Empty);
                //else
                //    email = email.Replace("${BlackDate}", Convert.ToDateTime(dt.Rows[0]["BlackDate"].ToString()).ToString("yyyy-MM-dd"));

                //if (dt.Rows[0]["CommentPromisedDate"] == null || dt.Rows[0]["CommentPromisedDate"].ToString() == String.Empty)
                //    email = email.Replace("${CommentPromisedDate}", String.Empty);
                //else
                //    email = email.Replace("${CommentPromisedDate}", Convert.ToDateTime(dt.Rows[0]["CommentPromisedDate"].ToString()).ToString("yyyy-MM-dd"));

                if (dt.Rows[0]["PromisedDate"] == null || dt.Rows[0]["PromisedDate"].ToString() == String.Empty)
                    email = email.Replace("${PromisedDate}", String.Empty);
                else
                    email = email.Replace("${PromisedDate}", Convert.ToDateTime(dt.Rows[0]["PromisedDate"].ToString()).ToString("yyyy-MM-dd"));

                if (dt.Rows[0]["SignDue"] == null || dt.Rows[0]["SignDue"].ToString() == String.Empty)
                    email = email.Replace("${SignDue}", String.Empty);
                else
                    email = email.Replace("${SignDue}", Convert.ToDateTime(dt.Rows[0]["SignDue"].ToString()).ToString("yyyy-MM-dd"));

                //if (dt.Rows[0]["ERPReceived"] == null || dt.Rows[0]["ERPReceived"].ToString() == String.Empty)
                //    email = email.Replace("${ERPReceived}", String.Empty);
                //else
                //    email = email.Replace("${ERPReceived}", Convert.ToDateTime(dt.Rows[0]["ERPReceived"].ToString()).ToString("yyyy-MM-dd"));

                //if (dt.Rows[0]["CEWPromised"] == null || dt.Rows[0]["CEWPromised"].ToString() == String.Empty)
                //    email = email.Replace("${CEWPromised}", String.Empty);
                //else
                //    email = email.Replace("${CEWPromised}", Convert.ToDouble(dt.Rows[0]["CEWPromised"].ToString()).ToString("N2"));

                //if (dt.Rows[0]["OrionPrice"] == null || dt.Rows[0]["OrionPrice"].ToString() == String.Empty)
                //    email = email.Replace("${OrionPrice}", String.Empty);
                //else
                //    email = email.Replace("${OrionPrice}", Convert.ToDouble(dt.Rows[0]["OrionPrice"].ToString()).ToString("N2"));

                //if (dt.Rows[0]["SalePrice"] == null || dt.Rows[0]["SalePrice"].ToString() == String.Empty)
                //    email = email.Replace("${SalePrice}", String.Empty);
                //else
                //    email = email.Replace("${SalePrice}", Convert.ToDouble(dt.Rows[0]["SalePrice"].ToString()).ToString("N2"));

                if (dt.Rows[0]["CEWME"] == null || dt.Rows[0]["CEWME"].ToString() == String.Empty)
                    email = email.Replace("${CEWME}", String.Empty);
                else
                    email = email.Replace("${CEWME}", Convert.ToDouble(dt.Rows[0]["CEWME"].ToString()).ToString("N2"));

                if (dt.Rows[0]["OrionME"] == null || dt.Rows[0]["OrionME"].ToString() == String.Empty)
                    email = email.Replace("${OrionME}", String.Empty);
                else
                    email = email.Replace("${OrionME}", Convert.ToDouble(dt.Rows[0]["OrionME"].ToString()).ToString("N2"));

                //if (dt.Rows[0]["GAP"] == null || dt.Rows[0]["CEWPromised"].ToString() == String.Empty)
                //    email = email.Replace("${GAP}", String.Empty);
                //else
                //    email = email.Replace("${GAP}", Convert.ToDouble(dt.Rows[0]["GAP"].ToString()).ToString("N2"));

                //if (dt.Rows[0]["SignActual"] == null || dt.Rows[0]["SignActual"].ToString() == String.Empty)
                //    email = email.Replace("${SignActual}", String.Empty);
                //else
                //    email = email.Replace("${SignActual}", Convert.ToDateTime(dt.Rows[0]["SignActual"].ToString()).ToString("yyyy-MM-dd"));

                //email = email.Replace("${ACRNo}", dt.Rows[0]["ACRNo"].ToString());
                //email = email.Replace("${AdvancedContract}", dt.Rows[0]["AdvancedContract"].ToString());
                //email = email.Replace("${CustomerName}", dt.Rows[0]["CustomerName"].ToString());
                //email = email.Replace("${OrionNo}", dt.Rows[0]["OrionNo"].ToString());
                email = email.Replace("${ProvinceCode}", dt.Rows[0]["ProvinceCode"].ToString());
                //email = email.Replace("${BGCode}", dt.Rows[0]["BGCode"].ToString());
                //email = email.Replace("${ACRNo}", dt.Rows[0]["ACRNo"].ToString());
                //email = email.Replace("${ASBorLucentName}", dt.Rows[0]["ASBorLucentName"].ToString());
                //email = email.Replace("${DeliveryPercent}", dt.Rows[0]["DeliveryPercent"].ToString());
                email = email.Replace("${Applicant}", dt.Rows[0]["Applicant"].ToString());
                //email = email.Replace("${ApplicantEmail}", dt.Rows[0]["ApplicantEmail"].ToString());
                email = email.Replace("${Principal}", dt.Rows[0]["Principal"].ToString());
                //email = email.Replace("${PrincipalEmail}", dt.Rows[0]["PrincipalEmail"].ToString());

                //if (dt.Rows[0]["ModifiedDate"] == null || dt.Rows[0]["ModifiedDate"].ToString() == String.Empty)
                //    email = email.Replace("${ModifiedDate}", String.Empty);
                //else
                //    email = email.Replace("${ModifiedDate}", Convert.ToDateTime(dt.Rows[0]["ModifiedDate"].ToString()).ToString("yyyy-MM-dd"));

                //email = email.Replace("${Risk}", dt.Rows[0]["IsRiskDesc"].ToString());

                //if (Convert.ToBoolean(dt.Rows[0]["Delivered"].ToString()))
                //    email = email.Replace("${Delivered}", "<input id='Checkbox1'  disabled='disabled' type='checkbox' checked='checked' />");
                //else
                //    email = email.Replace("${Delivered}", "<input id='Checkbox1'  disabled='disabled' type='checkbox'/>");


                //if (dt.Rows[0]["Creator"].ToString().Equals(String.Empty) || dt.Rows[0]["Creator"].ToString() == null)
                //{
                //    int commentStart = email.IndexOf("${Comment_Start}");
                //    int commentEnd = email.IndexOf("${Comment_END}");

                //    email = email.Remove(commentStart, (commentEnd - commentStart + 14));
                //}
                //else
                //{
                //    email = email.Replace("${Comment_Start}", null);
                //    email = email.Replace("${Creator}", dt.Rows[0]["Creator"].ToString());
                //    email = email.Replace("${CreatedDate}", dt.Rows[0]["DateCreated"].ToString());
                //    email = email.Replace("${Comments}", dt.Rows[0]["Comment"].ToString());
                //    email = email.Replace("${Comment_END}", null);
                //}

                if (dt.Rows[0]["PreContractStatus"].ToString() == "转正")
                    email = email.Replace("${Class}", "table_row_2");
                else
                {
                    if ((i % 2) == 0)
                        email = email.Replace("${Class}", "table_row_0");
                    else
                        email = email.Replace("${Class}", "table_row_1");
                }

                emailList += email;
            }

            emailHtm = emailHtm.Replace(emails, emailList);

            emailHtm = emailHtm.Replace("${TR_START}", String.Empty);
            emailHtm = emailHtm.Replace("${TR_END}", String.Empty);

            return emailHtm;
        }

        public static string SafeSQL(string parameter)
        {
            if (parameter == null)
                return string.Empty;

            parameter = parameter.Replace("'", " ");
            parameter = parameter.Replace(";", " ");
            parameter = parameter.Replace("%", " ");
            parameter = parameter.Replace("*", " ");
            parameter = parameter.Replace("\\", " ");
            parameter = parameter.Replace("&", " ");
            parameter = parameter.Replace(">", " ");
            parameter = parameter.Replace("<", " ");
            parameter = parameter.Replace("--", " ");

            return parameter;
        }

        /// <summary>
        /// Get dicItems by dicType
        /// </summary>
        /// <param name="droplist">DropDown List</param>
        /// <param name="dicType">DicItem Type</param>
        /// <param name="firstitemText">Default Value</param>
        /// <param name="hasFirstText">True or False</param>
        public static void BindDicItemDropDownListByDicType(DropDownList droplist, BEGDC.PreContract.BLL.DicType.DictionaryType dicType, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IDicItem itemservice = (BEGDC.PreContract.BLL.IDicItem)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicItem_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.DicItem.CONST_DicItemID;

            droplist.DataTextField = BEGDC.PreContract.DAL.DicItem.CONST_DicItemValue;

            droplist.DataSource = itemservice.GetDicItemByDicType(dicType);

            droplist.DataBind();
            if (hasFirstText)
            {
                droplist.Items.Insert(0, new ListItem(firstitemText, ""));
            }
        }

        public static void BindDicItemDropDownListByDicType(DropDownList droplist, int problemLV1ID)
        {
            BEGDC.PreContract.BLL.IDicItem itemservice = (BEGDC.PreContract.BLL.IDicItem)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicItem_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.DicItem.CONST_DicItemID;

            droplist.DataTextField = BEGDC.PreContract.DAL.DicItem.CONST_DicItemValue;

            droplist.DataSource = itemservice.GetProblemLV2ByLV1ID(problemLV1ID);

            droplist.DataBind();
        }

        //bind dic item type dropdown list
        public static void BindDicTypeDropDownList(DropDownList droplist, string firstitemText, bool hasFirstText)
        {
            BEGDC.PreContract.BLL.IDicType iDicType = (BEGDC.PreContract.BLL.IDicType)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_DicType_Service];

            droplist.DataValueField = BEGDC.PreContract.DAL.DicType.CONST_DicTypeID;

            droplist.DataTextField = BEGDC.PreContract.DAL.DicType.CONST_DicTypeDesc;

            droplist.DataSource = iDicType.GetDicType_DataTable(" IsSystem = 1");

            droplist.DataBind();
        }
    }

    public class PreContracColumn
    {
        #region Date

        public const string SignActual = "SignActual";
        public const string PromisedDate = "PromisedDate";
        public const string SignDue = "SignDue";
        public const string ERPReceived = "ERPReceived";
        public const string DeliveryDate = "DeliveryDate";
        public const string AOGDate = "AOGDate";
        public const string BlackDate = "BlackDate";
        public const string CommentPromisedDate = "CommentPromisedDate";

        #endregion

        #region Amount 

        public const string SparePrice = "SparePrice";
        public const string CEWPromised = "CEWPromised";
        public const string OrionPrice = "OrionPrice";
        public const string SalePrice = "SalePrice";
        public const string CFIPrice = "CFIPrice";
        public const string DeliveryPercent = "DeliveryPercent";
        public const string CEWME = "CEWME";
        public const string OrionME = "OrionME";
        public const string GAP = "GAP";

        #endregion

        #region Others

        public const string Delivered = "Delivered";
        public const string SpecialApprove = "SpecialApprove";
        
        #endregion
    }
}
